package com.lepu.app.fun.tool.ymsjb.bean;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;

public class BeanToolYiMiao {
	private static final String TABLE_NAME = "ToolYiMiao";
	
	public int id;
	public String uid; 
	public String time; // 月龄
    public String name; 
    public String number; //剂量
    public String note; //注释
    public int done; //是否


    //添加
    public static boolean insert(BeanToolYiMiao bean) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("uid", bean.uid);
        values.put("time", bean.time);
        values.put("name", bean.name);
        values.put("number", bean.number);
        values.put("note", bean.note);
        values.put("done", bean.done);
        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolYiMiao> getDataByUid(String uid) {
    	ArrayList<BeanToolYiMiao> provinces = new ArrayList<BeanToolYiMiao>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "time", "name", "number","note", "done"}, 
					"uid=?",new String[] {uid}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //获取所有数据
//    public static ArrayList<BeanToolYiMiao> getDataByDone(String uid, int done) {
//    	ArrayList<BeanToolYiMiao> provinces = new ArrayList<BeanToolYiMiao>();
//		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//		if (dbHelp == null)
//			return provinces;
//
//		SQLiteDatabase dbSqlite = null;
//		try {
//			dbSqlite = dbHelp.getWritableDatabase();
//
//			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
//					"uid=? and done=?",new String[] {uid,String.valueOf(done)}, null, null,null);
//
//			cursorParse(provinces, queryResult);
//			dbSqlite.close();
//		} catch (Exception e) {
//		}
//
//		return provinces;
//    }
    
    // 修改数据
    public static boolean update(BeanToolYiMiao bean) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        
        values.put("uid", bean.uid);
        values.put("time", bean.time);
        values.put("name", bean.name);
        values.put("number", bean.number);
        values.put("note", bean.note);
        values.put("done", bean.done);

        flag = dbHelp.update(TABLE_NAME, values, "uid=? and id=?", new String[]{bean.uid, String.valueOf(bean.id)});

        return flag;
    }
    
    
	//是否存在
	public static boolean isExist(String uid) {
		ArrayList<BeanToolYiMiao> provinces = new ArrayList<BeanToolYiMiao>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return false;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "time", "name", "number","note", "done"}, 
					"uid=?", new String[] { uid },null, null, null);
			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		if (provinces.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	//删除某一用户数据
    public static boolean deleteItem(String uid) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "uid=?",new String[]{uid});

        return flag;
    }
	
	private static void cursorParse(ArrayList<BeanToolYiMiao> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanToolYiMiao beanTarget = new BeanToolYiMiao();
			beanTarget.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanTarget.uid = (cursor.getString(cursor.getColumnIndex("uid")));
			beanTarget.time = (cursor.getString(cursor.getColumnIndex("time")));
			beanTarget.name = (cursor.getString(cursor.getColumnIndex("name")));
			beanTarget.number = (cursor.getString(cursor.getColumnIndex("number")));
			beanTarget.note = (cursor.getString(cursor.getColumnIndex("note")));
			beanTarget.done = (cursor.getInt(cursor.getColumnIndex("done")));
			msgArray.add(beanTarget);
		}
	}
	
	public static ArrayList<BeanToolYiMiao> getTestData(){
		ArrayList<BeanToolYiMiao> list = new ArrayList<BeanToolYiMiao>();
		
		BeanToolYiMiao bean1 = new BeanToolYiMiao();
		bean1.time = "2016-04-11";
		bean1.name = "甲肝疫苗";
		bean1.number = "1剂";
		bean1.note = "预防甲肝发生...";
		bean1.done = 0;
		
		BeanToolYiMiao bean2 = new BeanToolYiMiao();
		bean2.time = "2016-05-11";
		bean2.name = "乙肝疫苗";
		bean2.number = "2剂";
		bean2.note = "预防乙肝发生...";
		bean2.done = 1;
		
		list.add(bean1);
		list.add(bean2);
		
		return list;
	}
	
	
}
